package com.fh.address.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fh.address.entity.MemberReceiveAddress;
import com.fh.address.service.MemberReceiveAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import utils.ResultObj;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 会员收货地址表 前端控制器
 * </p>
 *
 * @author zs
 * @since 2021-10-20
 */
@RestController
@RequestMapping("/address")
public class MemberReceiveAddressController {
    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;
   @Autowired
   private RedisTemplate redisTemplate;
    @GetMapping
    public ResultObj getAddress(){
        QueryWrapper<MemberReceiveAddress> wrapper=new QueryWrapper<>();
        String userId = (String) redisTemplate.opsForValue().get("userId");
        wrapper.eq("member_id",userId);
        List<MemberReceiveAddress> list = memberReceiveAddressService.list(wrapper);
        return ResultObj.success(list);

    }
    @PostMapping
    public  ResultObj addAddress(@RequestBody MemberReceiveAddress address){
        String userId = (String) redisTemplate.opsForValue().get("userId");
        address.setMemberId(Long.parseLong(userId));
        memberReceiveAddressService.save(address);
        return ResultObj.success();

    }
    @DeleteMapping("{id}")
    public  ResultObj deleteAddress(@PathVariable("id") Long id){
        memberReceiveAddressService.removeById(id);
        return ResultObj.success();
    }

}
